/*
 * Copyright (C) 2010 Fred Barrie This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. This program is distributed
 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details. You should have received
 * a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.database.pojo;

import java.util.Calendar;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

import org.aergia.vinny.util.DateUtil;
import org.w3c.dom.Element;

/**
 * @author fred
 */
@Entity
public class SkillRate implements DatabasePojo {

	private boolean active;
	private Calendar endDate;
	private int id;
	private int rate;
	private Skill skill;
	private Calendar startDate;

	/**
	 * @param skill
	 * @param rate
	 */
	public SkillRate(final Skill skill, final int rate) {
		this.skill = skill;
		this.rate = rate;
		this.startDate = DateUtil.getInstance();
		this.endDate = DateUtil.FAR_IN_FUTURE;
	}

	/**
	 * @param skill
	 * @param rate
	 * @param startDate
	 */
	public SkillRate(final Skill skill, final int rate, final Calendar startDate) {
		this.skill = skill;
		this.rate = rate;
		this.startDate = startDate;
		this.endDate = DateUtil.FAR_IN_FUTURE;
	}

	protected SkillRate() {
		this(null, 0);
	}

	@Override
	public void attachAttributes(final Element element) {
		element.setAttribute("id", Integer.toString(this.getId()));
		element.setAttribute("active", Boolean.toString(this.isActive()));
		element.setAttribute("endDate", DateUtil.format(this.getEndDate()));
		element.setAttribute("rate", Integer.toString(this.getRate()));
		element.setAttribute("startDate", DateUtil.format(this.getStartDate()));
		element.setAttribute("skill", Integer.toString(this.getSkill().getId()));
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof SkillRate)) {
			return false;
		}
		final SkillRate other = (SkillRate) obj;
		if (this.getEndDate() == null) {
			if (other.getEndDate() != null) {
				return false;
			}
		} else if (!this.getEndDate().equals(other.getEndDate())) {
			return false;
		}
		if (this.getRate() != other.getRate()) {
			return false;
		}
		if (this.getSkill() == null) {
			if (other.getSkill() != null) {
				return false;
			}
		} else if (!this.getSkill().equals(other.getSkill())) {
			return false;
		}
		if (this.getStartDate() == null) {
			if (other.getStartDate() != null) {
				return false;
			}
		} else if (!this.getStartDate().equals(other.getStartDate())) {
			return false;
		}
		return true;
	}

	/**
	 * @return the endDate
	 */
	public Calendar getEndDate() {
		return this.endDate;
	}

	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	/**
	 * @return the rate
	 */
	public int getRate() {
		return this.rate;
	}

	/**
	 * @return the skill
	 */
	@ManyToOne
	public Skill getSkill() {
		return this.skill;
	}

	/**
	 * @return the startDate
	 */
	public Calendar getStartDate() {
		return this.startDate;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (this.getEndDate() == null ? 0 : this.getEndDate().hashCode());
		result = prime * result + this.getRate();
		result = prime * result + (this.getSkill() == null ? 0 : this.getSkill().hashCode());
		result = prime * result + (this.getStartDate() == null ? 0 : this.getStartDate().hashCode());
		return result;
	}

	@Override
	public boolean isActive() {
		return this.active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(final boolean active) {
		this.active = active;
	}

	/**
	 * @param endDate
	 *            the endDate to set
	 */
	public void setEndDate(final Calendar endDate) {
		this.endDate = endDate;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(final int id) {
		this.id = id;
	}

	/**
	 * @param rate
	 *            the rate to set
	 */
	public void setRate(final int rate) {
		this.rate = rate;
	}

	/**
	 * @param skill
	 *            the skill to set
	 */
	public void setSkill(final Skill skill) {
		this.skill = skill;
	}

	/**
	 * @param startDate
	 *            the startDate to set
	 */
	public void setStartDate(final Calendar startDate) {
		this.startDate = startDate;
	}

	@Override
	public String toString() {
		return "SkillRate [active=" + this.isActive() + ", endDate=" + DateUtil.format(this.getEndDate()) + ", rate=" + this.getRate() + ", skill="
				+ this.getSkill() + ", startDate=" + DateUtil.format(this.getStartDate()) + "]";
	}
}
